/*
    定义线程管理相关函数以及接口类
*/
#ifndef MYTHREAD_HEAD_DEFINE
#define MYTHREAD_HEAD_DEFINE

#include <pthread.h>
#include <iostream>
#include "table.h"

// 功能函数接口类，通过接口实现添加记录和查询功能
class FunctionInterface{
public:
    FunctionInterface(){}
    virtual ~FunctionInterface(){}
public:
    virtual void RunFunction() = 0; // 这里封装功能函数
};

// 实现查询功能接口
class FunctionSearch:public FunctionInterface{
public:

    FunctionSearch(int lowerBound, int upperBound, int col){
        threadLowerBound = lowerBound;
        threadUpperBound = upperBound;
        threadCol = col;
    }
    virtual ~FunctionSearch(){}
    virtual void RunFunction(){
        table* threadTable = table::GetTable();
        threadTable->SearchRecord(threadLowerBound, threadUpperBound, threadCol);
    }

private:
    //查询信息
    int threadLowerBound;
    int threadUpperBound;
    int threadCol;
};

// 实现添加记录功能接口
class FunctionInsert: public FunctionInterface {
public:
    FunctionInsert() {}

    virtual ~FunctionInsert() {}

    virtual void RunFunction() {
        table* threadTable = table::GetTable();
        threadTable->InsertRecord();
    }
};

// 创建线程功能的接口
class FunctionThreadInterface{
public:
    explicit FunctionThreadInterface(FunctionInterface* pFunctionInterface){
        pThreadFunctionInterface=pFunctionInterface;
    }
    virtual ~FunctionThreadInterface(){}
    virtual void Run()=0;
    virtual void WaitForDeath()=0;

protected:
    FunctionInterface* pThreadFunctionInterface;

};

// 实现创建线程的接口
class FunctionThread : public FunctionThreadInterface
{
public:
    explicit FunctionThread(FunctionInterface* pFunctionInterface);
    virtual ~FunctionThread();
    virtual void Run();
    virtual void WaitForDeath();

private:
    static void* StartFunctionOfThread(void* pThis);

private:
    pthread_t threadID;
};


#endif // MYTHREAD_HEAD_DEFINE
